package com.servlet.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.UserDao;
import com.entity.User;

@WebServlet("/user/save")
public class UserSaveServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	super.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	String id=req.getParameter("id");
	String username=req.getParameter("username");
	String password=req.getParameter("password");
	User user=null;
	UserDao dao=new UserDao();
	//修改
	if(!id.equals("")) {
	    user=dao.findById(Integer.parseInt(id));
	    user.setUsername(username);
	    user.setPassword(password);
	    dao.edit(user, Integer.parseInt(id));
	//新增    
	}else {
	    user=new User();
	    user.setUsername(username);
	    user.setPassword(password);
	    dao.add(user);
	}
	if(user!=null) {
	   resp.getWriter().write("200"); 
	}
	
    }
    
    

}
